package com.ftg.learn.chapter05;


import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 键
 *
 * @author 布衣
 */
public class MyEntry<K, V> {

    private Entry<K, V>[] entrys;

    public MyEntry() {
        entrys = new Entry[16];
    }

    int size;

    public boolean put(K k1, V v1) {
        Entry<K, V> entry1 = new Entry<>(k1, v1,null);
        //o1 key 做文章 key : 变成一个数字
        //h 就是hash值
        // acD abc
        int h = k1.hashCode();
        int index = h & entrys.length - 1;
        //有没有可能这个你算出来的下标，里面有人了.
        if(entrys[index] != null && entrys[index].key != k1){
            //链表尾插法
            Entry<K, V> temp = entrys[index];
            while(temp.next!=null){
                if(temp.key == k1){
                    temp.val = v1;
                    return true;
                }
                temp = temp.next;
            }
            temp.next = entry1;
            size++;
            return true;
        }
        entrys[index] = entry1;
        return true;
    }

    public V get(K k1) {
        int h = k1.hashCode();
        int index = h & entrys.length - 1;
        Entry<K,V> temp = entrys[index];
        while(temp != null){
            if(temp.key == k1){
                return temp.val;
            }
            temp = temp.next;
        }
        return null;
    }

    //jdk1.61.5 1.4 恭喜您  1.7
    public static void main(String[] args) {

        MyEntry<String, String> myEntry = new MyEntry<>();
        myEntry.put("abc", "aaaaa");
        myEntry.put("acD", "bbbbb");
        myEntry.put("adc", "ccccc");
        System.out.println(myEntry.get("adc"));

        Map<String,String> map1 = new HashMap<>(3);


        Set<String> sets = new HashSet<>(3);
        Set<String> set1 = new TreeSet<>();
        Set<String> set12 = new LinkedHashSet<>();
        sets.add("abc");
        sets.add("abc");
        sets.add("abc");
//        tableForsize(3 + 3>>>1 + 1);
        Map<String,String> map3 = new ConcurrentHashMap<>(3);
        map3.put("a","afdasfdasfds");

    }

    /**
     * 成员内部类
     * 键  ： key | value k-v对
     * key 唯一是什么意思
     */
    private class Entry<K, V> {
        private K key;
        private V val;
        private Entry<K,V> next;

        public Entry(K k1, V v1, Entry<K,V> next) {
            this.key = k1;
            this.val = v1;
            this.next = next;
        }
    }


}
